package com.example.jinmingwu.a180607_handler;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;

public class MainActivity extends AppCompatActivity {
    private Button runnableStartButton = null;
    private Button runnableEndButton = null;
    private Button messageStartButton = null;
    private ProgressBar progressbar = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        runnableStartButton = (Button) findViewById(R.id.runnableStart);
        runnableStartButton.setOnClickListener(new runnableStartButtonListener());
        runnableEndButton = (Button) findViewById( R.id.runnableEnd);
        runnableEndButton.setOnClickListener(new runnableEndButtonListenet());
        messageStartButton = (Button)findViewById(R.id.messageStart);
        messageStartButton.setOnClickListener(new messageStartButtonListener());
        progressbar = (ProgressBar)findViewById(R.id.progrssbar);
    }

    Handler handler = new Handler();

    class runnableStartButtonListener implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            handler.post(updateThread);
        }
    }

    class runnableEndButtonListenet implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            handler.removeCallbacks(updateThread);
            updateBarHandler.removeCallbacks(updateThread2);
        }
    }

    Runnable updateThread = new Runnable() {
        @Override
        public void run() {
            System.out.println("UpdateThread hhhhh");
            Log.v("tag","updateThread");
            handler.postDelayed(updateThread,2000);
        }
    };

    public int i = 0;

    class messageStartButtonListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            i = 0;
            progressbar.setVisibility(View.VISIBLE);
            updateBarHandler.post(updateThread2);
        }
    }

   Handler updateBarHandler = new Handler(){
        public void handleMessage(Message msg) {
            progressbar.setProgress(msg.arg1);
            updateBarHandler.post(updateThread2);
        }
    };



    Runnable updateThread2 =new Runnable() {
        @Override
        public void run() {
            if(i == 100){
                updateBarHandler.removeCallbacks(updateThread2);
                System.out.println("Stop Stop Stop!!!");
            }
            System.out.println("Begin Thread" + i);
            i += 1;
            Message msg = updateBarHandler.obtainMessage();
            msg.arg1 = i;
            try {
                Thread.sleep(100);
            } catch(InterruptedException e){
                e.printStackTrace();
            }
            updateBarHandler.sendMessage(msg);

        }
    };
}
